package com.bt.springboot.demo.io;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import com.bt.springboot.common.util.IMUtil;
import com.bt.springboot.common.util.PinyinUtil;
import com.bt.springboot.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author zkx
 * @Date 2022/7/4 17:38
 */
@Slf4j
public class IOStreamDemo {


	public static void main(String[] args) throws IOException {
		// Java IO流实现分行向文件中写入数据
//		fw.writeData(filePath);
		// Java IO流实现从文件中读出数据
//		fw.readData(filePath);
		IOStreamDemo ioStreamDemo = new IOStreamDemo();
		ioStreamDemo.personScript();
//		ioStreamDemo.urlScript();
//		ioStreamDemo.complaintScript();
//		ioStreamDemo.scrmMtsLeadsScript();
//		ioStreamDemo.imSignScript();
//		ioStreamDemo.susiteIdScript();
//		ioStreamDemo.lossDataScript();
//		ioStreamDemo.accountAbbrScript();
	}

	private void personScript() {
		String filePath1 = "C:\\Users\\SuperX\\Desktop\\中原业务部.json";
		String filePath2 = "C:\\Users\\SuperX\\Desktop\\心理业务部.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s1 = fw.readJsonFile(filePath1);
		SqlExport sqlExport1 = JSONUtil.toBean(s1, SqlExport.class);
		List<Person> personList1 = sqlExport1.getPersonList();

		String s2 = fw.readJsonFile(filePath2);
		SqlExport sqlExport2 = JSONUtil.toBean(s2, SqlExport.class);
		List<Person> personList2 = sqlExport2.getPersonList();

		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\personUpdate.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (Person person : personList2) {
				Person person1 = personList1.stream().filter(p -> p.getSalesCrmId().equals(person.getSalesCrmId())).findAny().orElse(null);


				String content = "update dws_sales_perform_res set department = '" + person1.getDepartment() + "'," +
						" center = '" + person1.getCenter() + "'" +
						" where id = " + person.getId() + ";";
				// 写入数据到输出流
				bw.write(content);
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	private void urlScript() {
		String filePath = "C:\\Users\\SuperX\\Desktop\\url-prod.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s = fw.readJsonFile(filePath);
		SqlExport sqlExport = JSONUtil.toBean(s, SqlExport.class);
		List<AdsUrl> adsUrlList = sqlExport.getAdsUrlList();

		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\updateAdsUrl-prod.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (AdsUrl adsUrl : adsUrlList) {
				UrlBuilder urlBuilder = UrlBuilder.of(adsUrl.getUrl(), StandardCharsets.UTF_8);
				String subUrl = urlBuilder.getHost() + StringUtils.substringBefore(urlBuilder.getPath().toString(),".");
				String content = "update ads_url set sub_url = '" + subUrl + "' where id = " + adsUrl.getId() + ";";
				// 写入数据到输出流
				bw.write(content);
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}


	private void complaintScript() {
		String filePath = "C:\\Users\\SuperX\\Desktop\\客诉.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s = fw.readJsonFile(filePath);
		SqlExport sqlExport = JSONUtil.toBean(s, SqlExport.class);
		List<ComplaintWorkOrder> complaintWorkOrderList = sqlExport.getComplaintWorkOrderList();

		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\updateAssignTime.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (ComplaintWorkOrder complaintWorkOrder : complaintWorkOrderList) {
				// 写入数据到输出流
				bw.write("update complaint_work_order set assign_time = '" + complaintWorkOrder.getAssignTime() + "' where id = " + complaintWorkOrder.getId() + ";");
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	private void scrmMtsLeadsScript(){
		String filePath = "C:\\Users\\SuperX\\Desktop\\mtsLeads.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s = fw.readJsonFile(filePath);
		SqlExport sqlExport = JSONUtil.toBean(s, SqlExport.class);
		List<ScrmLeads> scrmLeadsList = sqlExport.getScrmLeadsList();
		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\updateMtsLeads.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (ScrmLeads scrmLeads : scrmLeadsList) {
				// 写入数据到输出流
				// 请查看示例 https://gist.github.com/retanoj/5fd369524a18ab68a4fe7ac5e0d121e8
				bw.write("update scrm_leads set outer_sys_id = '" + scrmLeads.getMtsUUId() +  "' where id = " + scrmLeads.getId() + ";");
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	private void imSignScript(){
		String filePath = "C:\\Users\\SuperX\\Desktop\\imUser1.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s = fw.readJsonFile(filePath);
		SqlExport sqlExport = JSONUtil.toBean(s, SqlExport.class);
		List<ImUserPool> imUserPoolList = sqlExport.getImUserPoolList();
		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\updateSig1.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (ImUserPool imUserPool : imUserPoolList) {
				// 写入数据到输出流
				bw.write("update im_user_pool set im_pwd = '" + IMUtil.genUserSig(imUserPool.getImUser()) +  "' where id = " + imUserPool.getId() + ";");
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	private void lossDataScript() {
		String filePath = "C:\\Users\\SuperX\\Desktop\\流失数据.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s = fw.readJsonFile(filePath);
		SqlExport sqlExport = JSONUtil.toBean(s, SqlExport.class);
		List<LossData> lossDataList = sqlExport.getLossDataList();

		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\updateLossTime.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (LossData lossData : lossDataList) {
				// 写入数据到输出流
				bw.write("update scrm_leads set loss_type = '" + lossData.getLossType() + "' , loss_time = '" + lossData.getLossTime() + "' where id = " + lossData.getLeadsId() + ";");
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	private void susiteIdScript() {
		String filePath = "C:\\Users\\SuperX\\Desktop\\subsiteId.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s = fw.readJsonFile(filePath);
		SqlExport sqlExport = JSONUtil.toBean(s, SqlExport.class);
		List<AdsPlans> records = sqlExport.getAdsPlansList();
		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\updateSubsiteSql.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (AdsPlans adsPlans : records) {
				// 写入数据到输出流
				bw.write("update ads_plans set subsite_id = '" + adsPlans.getSubsiteId() + "' where id = " + adsPlans.getId() + ";");
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	private void accountAbbrScript() {
		String filePath = "C:\\Users\\SuperX\\Desktop\\无标题.json";
		IOStreamDemo fw = new IOStreamDemo();
		String s = fw.readJsonFile(filePath);
		SqlExport sqlExport = JSONUtil.toBean(s, SqlExport.class);
		List<AdsAccount> records = sqlExport.getAdsAccountList();

		// 写入文件
		try (// 创建文件输出流
			 FileWriter fw2 = new FileWriter("C:\\Users\\SuperX\\Desktop\\updateAccAbbr.sql");
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw2)) {

			//循环写入100行数据
			for (AdsAccount adsAccount : records) {
				// 写入数据到输出流
				String accountAbbr = PinyinUtil.toFirstChar(adsAccount.getUserName());
				bw.write("update ads_account set account_abbr = '" + accountAbbr + "' where id = " + adsAccount.getId() + ";");
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	/**
	 * Java IO流实现分行向文件中写入数据
	 * <p>
	 * FileWriter类可以向文件写入字符数据，如果将FileWriter类封装到BufferWriter类的缓冲字符流中，能够实现缓冲字符输出流，
	 * 并且可以通过读输出流的newLine()方法，来实现数据的分行写入。
	 *
	 * @param filePath 文件路径
	 */
	public void writeData(String filePath) {
		File file = new File(filePath);

		try (// 创建文件输出流
			 FileWriter fw = new FileWriter(file);
			 // 使用缓冲区数据流封装输出流
			 BufferedWriter bw = new BufferedWriter(fw)) {

			//循环写入100行数据
			for (int i = 0; i < 100; i++) {
				// 写入数据到输出流
				bw.write("Java交互管道——IO流");
				// 写入换行符
				bw.newLine();
				// 刷新缓冲区
				bw.flush();
			}

			log.info("成功写入数据！");

		} catch (IOException e) {
			log.error("", e);
		}
	}

	/**
	 * Java IO流实现从文件中读出数据
	 *
	 * @param filePath 文件路径
	 */
	public void readData(String filePath) {
		try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
			String s;
			while ((s = br.readLine()) != null) {
				log.info(s);
			}
		} catch (Exception e) {
			log.error("", e);
		}

	}

	public String readJsonFile(String filePath) {
		String jsonStr = "";
		try {
			File jsonFile = new File(filePath);
			Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
			int ch = 0;
			StringBuffer sb = new StringBuffer();
			while ((ch = reader.read()) != -1) {
				sb.append((char) ch);
			}
			reader.close();
			jsonStr = sb.toString();
			return jsonStr;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
